<?php
$sort = $this->sort;
$sort_name = array("class"=>"","rel"=>"asc");
$sort_date = array("class"=>"","rel"=>"asc");

switch($sort['by']){
    case "fullname": 
        $sort_name = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "date": 
       $sort_date = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
}
?>
<table class="data display datatable">
    <thead>
        <tr>
            <th width="50" class="center">STT</th>
            <th class="sorting<?=$sort_name['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"contact","action"=>"list","sortby"=>"fullname"))?>" rel="<?=$sort_name["rel"]?>">Tên người liên hệ</a>
            </th>
            <th width="100" class="sorting<?=$sort_date['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"contact","action"=>"list","sortby"=>"date"))?>" rel="<?=$sort_date['rel']?>">Ngày liên hệ</a>
            </th>
            <th width="50" class="center">Xem</th>
            <th  width="50"  class="center">Xóa</th>
        </tr>                  
    </thead>
    
    <tbody>
        <?php $stt=1; foreach($this->datas as $row){ $parity = ($stt%2)?"odd":"even";?>
        <tr class="gradeA <?=$parity?>">
            <td><?=$stt?></td>
            <td><?=$row->fullname?></td>
             <?php
            $date = new Zend_Date($row->date);
            $contact_date = $date->toString("dd-MM-Y");
            ?>
            <td><?=$contact_date?></td>
            <td class="center">
                <a style="color: blue;" class="view" href="<?=$this->url(array("module"=>"admin","controller"=>"contact","action"=>"view","id"=>$row->idcontacts))?>">
                    [Xem]
                </a>
            </td>
            <td>
                <a class="del" href="<?=$this->url(array("module"=>"admin","controller"=>"contact","action"=>"del","id"=>$row->idcontacts))?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/icon-del.png")?>" alt="[Xóa]" title="Xóa"/>
                </a>
            </td>
        </tr>
        <?php $stt++;}?>
    </tbody>
</table>
<?php if($this->datas->count()>1){// Nhiều hơn 1 trang thì sử dụng phân trang?>
<div class="dataTables_paginate paging_full_numbers">
    <?=$this->paginationControl($this->datas,"Sliding","partial/pagination-control.phtml")?>
</div>
<?php }?>
<div class="dialog-message" title="Thông báo" style="display: none;"></div>
<div class="dialog-view" title="" style="display: none;"></div>
<script>
$(function(){    
    /*Xóa bài liên hệ*/
    $(".del").click(function(){
        var _this = $(this);
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo</h5><p>Bạn có chắc muốn thực hiện hành động này không?</p></div>")
                            .dialog({            
            autoOpen: true,
            modal: true,
            buttons: {
                "Có": function(){                    
                    var link =  _this.attr("href");
                    _this.parent("td").parent("tr").remove();
                    $.get(link,{},function(){},"json");
                    $(this).dialog("close");
                },
                "Không": function(){
                    $(this).dialog("close");
                }
            }
        })
        return false;
    })
    
    
     /*Phân trang*/
    $(".pagination a").click(function(){
        var link = $(this).attr("href");
        $(".loading").show();
        $.get(link,null,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    /*Sắp xếp*/
    $(".sorting a").click(function(){
        var link = $(this).attr("href");
        var type = $(this).attr("rel");
        var filter_value = $(".searchkey").val();
        var sdata = {"sorttype":type,"filter_field":"fullname","filter_value":filter_value};
        if(filter_value.length==0){
            sdata={"sorttype":type};
        }
        $(".loading").show();
        $.post(link,sdata,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    $(".view").click(function(){
        var clickObj = $(this);
        var link = $(this).attr("href");
        $.get(link,null,function(rdata){
            var html = "<div class='message info'><h5>Chi tiết liên hệ</h5>";
            html    +=      "<p class='detail-info odd'><label>Họ tên:</label><span class='uppercase'>"+rdata.fullname+"</span></p>";
            html    +=      "<p class='detail-info even'><label>Điện thoại:</label><span>"+rdata.phone+"</span></p>";
            html    +=      "<p class='detail-info odd'><label>Email:</label><span>"+rdata.email+"</span></p>";
            html    +=      "<p class='detail-info even'><label>Tiêu đề:</label><span>"+rdata.title+"</span></p>";
            html    +=      "<p class='detail-info odd'><label>Nội dung:</label><span>"+rdata.content+"</span></p>";
            html    +=      "<p class='detail-info even'><label>Ngày gởi:</label><span>"+rdata.date+"</span></p>";
            html    += "</div>";   
            $(".dialog-view").html(html).dialog({
                autoOpen: true,
                modal: true,
                width: 700,
                title: rdata.fullname + " liên hệ",
                buttons:{
                    "Đóng": function(){ $(this).dialog("close")}
                }
            });
        },"json");
        return false;
    })
    
})
</script>